package com.numbfish.ch16_ex05_reactor_start;

import com.numbfish.ch16_ex05_reactor_start.config.CustomConfig;
import com.numbfish.ch16_ex05_reactor_start.pojo.Person;
import lombok.extern.slf4j.Slf4j;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import reactor.core.publisher.Flux;

import java.util.Arrays;


@Slf4j
public class Ch16Ex05ReactorStartTestApp {

    @Test
    public void test1() {
        Flux<Integer> just = Flux.just(1, 2, 3, 4, 5);
        just.subscribe(System.out::println);
    }

    @Test
    public void test2() {
        Flux<Integer> just = Flux.range(1, 7); // 双闭区间
        just.subscribe(System.out::println);
    }

    @Test
    public void test3() {
        Flux<Integer> just = Flux.range(1, 7); // 双闭区间
        just.map(i -> i + 1).subscribe(System.out::println);
    }

    @Test
    public void test4() {
        Flux<String> flux = Flux.just("apple", "banana", "cherry");

        flux.doOnNext(System.out::println)
                .subscribe();
    }

    @Test
    public void test5() {
        Flux<Integer> flux = Flux.just(1, 3, 4, 5, 6, 7, 8, 8, 10).map(v -> {
            if (v == 7) {
                return 10 / (7 - v); // 7 的时候抛异常
            }
            return v;
        });

        flux.subscribe(item -> {
            System.out.println(item + Thread.currentThread().getName());
        }, e -> {
            System.out.println(Thread.currentThread().getName());
            e.printStackTrace();
        }); // 正常就输出，异常就打印堆栈
        // 第二个流处理不会被第一个流处理的异常影响，程序不会因为第一个流处理的异常而终止。
        flux.subscribe(item -> {
            System.out.println(item + Thread.currentThread().getName());
        }, e -> {
            System.out.println("throwable = " + e+Thread.currentThread().getName());
        },()->{
            System.out.println("正常结束");
        }); // 正常就输出，异常就打印throwable
    }
}